<?php

/**
 * This is the model class for table "tbl_item".
 *
 * The followings are the available columns in table 'tbl_item':
 * @property integer $id
 * @property string $type
 * @property string $model
 * @property string $manufacterID
 * @property string $status
 * @property integer $id_batch
 * @property integer $id_unit
 * @property string $notes
 *
 * The followings are the available model relations:
 * @property Unit $idUnit
 * @property BenefactorBatch $idBatch
 */
class Item extends CActiveRecord
{
	const TYPE_CASING = 0;
	const TYPE_CD_DRIVE = 1;
	const TYPE_DVD_DRIVE = 2;
	const TYPE_FLOPPY_DRIVE = 3;
	const TYPE_HDD = 4;
	const TYPE_KEYBOARD = 5;
	const TYPE_MODEM = 6;
	const TYPE_MONITOR = 7;
	const TYPE_MOTHERBOARD = 8;
	const TYPE_MOUSE = 9;
	const TYPE_POWER_SUPPLY = 10;
	const TYPE_PRINTER = 11;
	const TYPE_PROCESSOR = 12;
	const TYPE_RAM = 13;
	const TYPE_SCANNER = 14;
	const TYPE_SPEAKER = 15;
	const TYPE_VGA = 16;

	const STATUS_UNKNOWN = 0;
	const STATUS_WORKING = 1;
	const STATUS_BROKEN = 2;
	
	
	/**
	 * Returns the static model of the specified AR class.
	 * @return Item the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_item';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('id_batch, id_unit', 'numerical', 'integerOnly'=>true),
			array('type, model, manufacterID, status', 'length', 'max'=>256),
			array('notes', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, type, model, manufacterID, status, id_batch, id_unit, notes', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'idUnit' => array(self::BELONGS_TO, 'Unit', 'id_unit'),
			'idBatch' => array(self::BELONGS_TO, 'BenefactorBatch', 'id_batch'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'type' => 'Type',
			'model' => 'Model',
			'manufacterID' => 'Manufacter',
			'status' => 'Status',
			'id_batch' => 'Id Batch',
			'id_unit' => 'Id Unit',
			'notes' => 'Notes',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('type',$this->type,true);
		$criteria->compare('model',$this->model,true);
		$criteria->compare('manufacterID',$this->manufacterID,true);
		$criteria->compare('status',$this->status,true);
		$criteria->compare('id_batch',$this->id_batch);
		$criteria->compare('id_unit',$this->id_unit);
		$criteria->compare('notes',$this->notes,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	/**
	* @return array item type indexed by type ID
	*/
	public function getTypeOptions()
	{
		return array(
			self::TYPE_CASING => 'Casing',
			self::TYPE_CD_DRIVE => 'CD Drive',
			self::TYPE_DVD_DRIVE => 'DVD Drive',
			self::TYPE_FLOPPY_DRIVE => 'Floppy Drive',
			self::TYPE_HDD => 'Harddisk Drive',
			self::TYPE_KEYBOARD => 'Keyboard',
			self::TYPE_MODEM => 'Modem',
			self::TYPE_MONITOR => 'Monitor',
			self::TYPE_MOUSE => 'Mouse',
			self::TYPE_MOTHERBOARD => 'Motherboard',
			self::TYPE_POWER_SUPPLY => 'Power Supply',
			self::TYPE_PRINTER => 'Printer',
			self::TYPE_PROCESSOR => 'Processor',
			self::TYPE_RAM => 'RAM',
			self::TYPE_SCANNER => 'Scanner',
			self::TYPE_SPEAKER => 'Speaker',
			self::TYPE_VGA => 'VGA',
		);	
	}
	
	/**
	* @return array item status indexed by type ID
	*/
	public function getStatusOptions()
	{
		return array(
			self::STATUS_UNKNOWN => 'Unknown',
			self::STATUS_WORKING => 'Working',
			self::STATUS_BROKEN => 'Broken',
		);
	}
	
	/**
	* @return string the type text display for the current issue
	*/ 
	public function getTypeText()
	{
		$typeOptions=$this->typeOptions;
		return isset($typeOptions[$this->type]) ? $typeOptions[$this->type] : "unknown type ({$this->type})";
	}
	
	/**
	* @return string the status text display for the current issue
	*/ 
	public function getStatusText()
	{
		$statusOptions=$this->statusOptions;
		return isset($statusOptions[$this->status]) ? $statusOptions[$this->status] : "unknown type ({$this->status})";
	}
}